package org.edu.modules.kwgl.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.kwgl.config.ConfigConstant;
import org.edu.modules.kwgl.entity.VeJwChengji;
import org.edu.modules.kwgl.entity.VeJwCjChange;
import org.edu.modules.jxgz.entity.VeJwText;
import org.edu.modules.kwgl.service.IVeJwChengjiService;
import org.edu.modules.kwgl.service.IVeJwCjChangeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.kwgl.utils.JwTextUtils;
import org.edu.modules.scheduleCommon.dto.PageDto;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 成绩修改申请
 * @Author: jeecg-boot
 * @Date: 2021-05-12
 * @Version: V1.0
 */
@Api(tags = "成绩修改申请")
@RestController
@RequestMapping("/kwgl/veJwCjChange")
@Slf4j
public class VeJwCjChangeController extends BaseController<VeJwCjChange, IVeJwCjChangeService> {
    @Autowired
    private IVeJwCjChangeService veJwCjChangeService;

    @Autowired
    private IVeJwChengjiService veJwChengjiService;

    /**
     * 分页列表查询
     *
     * @param veJwCjChange
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "成绩修改申请-分页列表查询")
    @ApiOperation(value = "成绩修改申请-分页列表查询", notes = "成绩修改申请-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwCjChange veJwCjChange,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJwCjChange> queryWrapper = QueryGenerator.initQueryWrapper(veJwCjChange, req.getParameterMap());
//        Page<VeJwCjChange> page = new Page<VeJwCjChange>(pageNo, pageSize);
//        IPage<VeJwCjChange> pageList = veJwCjChangeService.page(page, queryWrapper);
        PageDto<VeJwCjChange> page = new PageDto<>(pageNo, pageSize);
        IPage<VeJwCjChange> pageList = veJwCjChangeService.pageDto(page, queryWrapper);

        //获取原成绩信息
        for (VeJwCjChange item : pageList.getRecords()) {
            VeJwChengji chengji = veJwChengjiService.getById(item.getCjid());
            if (chengji != null) {
                item.setChengji(chengji);
            }
            item.setAudittidText(JwTextUtils.getText(item.getAudittid()));
            item.setReasontidText(JwTextUtils.getText(item.getReasontid()));
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param veJwCjChange
     * @return
     */
    @AutoLog(value = "成绩修改申请-添加")
    @ApiOperation(value = "成绩修改申请-添加", notes = "成绩修改申请-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwCjChange veJwCjChange) {
        if (veJwCjChange.getKccj() == null && veJwCjChange.getKcdjcjm() == null) {
            return Result.error("请求参数有误！");
        }
        veJwCjChange.setReasontid(JwTextUtils.addText(veJwCjChange.getReasontidText()));
        long l = System.currentTimeMillis() / 1000;
        int now = new Long(l).intValue();
        veJwCjChange.setAuditstatus(0);
        veJwCjChange.setAudittime(now);
        //查看是否已存在申请修改信息
        VeJwCjChange change = veJwCjChangeService.getLatestInfoBy2id(veJwCjChange.getStuid(), veJwCjChange.getKsid());
        if (change == null) {
            //如果是考试形式为【考试】
            if (veJwCjChange.getKsxsid() == 1) {
                //分数超过100置为0
                if (veJwCjChange.getKccj().compareTo(new BigDecimal("100.0")) > 0) {
                    veJwCjChange.setKccj(new BigDecimal("0.0"));
                }
            }
            veJwCjChangeService.save(veJwCjChange);
            return Result.ok("申请修改成功！");
        }
        //判断已存在的申请修改信息状态
        if (change.getAuditstatus() == 0) {
            return Result.error("申请仍在审核中，请勿重复提交！");
        } else {
            veJwCjChangeService.save(veJwCjChange);
        }
        return Result.ok("申请修改成功！");
    }

    /**
     * 编辑
     *
     * @param veJwCjChange
     * @return
     */
    @AutoLog(value = "成绩修改申请-编辑")
    @ApiOperation(value = "成绩修改申请-编辑", notes = "成绩修改申请-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwCjChange veJwCjChange) {
        veJwCjChangeService.updateById(veJwCjChange);
        return Result.ok("编辑成功!");
    }


    /**
     * 审核
     *
     * @param veJwCjChange
     * @return
     */
    @AutoLog(value = "成绩修改申请-审核")
    @ApiOperation(value = "成绩修改申请-审核", notes = "成绩修改申请-审核")
    @PutMapping(value = "/audit")
    public Result<?> audit(@RequestBody VeJwCjChange veJwCjChange) {

        VeJwCjChange old = veJwCjChangeService.getById(veJwCjChange.getId());
        if (old == null) {
            return Result.error("记录不存在!");
        }
//		 if(old.getAuditstatus() == ConfigConstant.AUDIT_AGREE){
//			 return Result.error("已通过审核!");
//		 }
        if (veJwCjChange.getAuditstatus() != ConfigConstant.AUDIT_AGREE && veJwCjChange.getAudittidText() == null) {
            return Result.error("请填写不通过理由!");
        }
        VeJwCjChange update = new VeJwCjChange();
        update.setId(veJwCjChange.getId());
        update.setAuditstatus(veJwCjChange.getAuditstatus());
        if (veJwCjChange.getAudittidText() != null && veJwCjChange.getAudittidText().length() > 0) {
            update.setAudittid(JwTextUtils.addText(veJwCjChange.getAudittidText()));
        }


        veJwCjChangeService.updateById(update);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "成绩修改申请-通过id删除")
    @ApiOperation(value = "成绩修改申请-通过id删除", notes = "成绩修改申请-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        veJwCjChangeService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "成绩修改申请-批量删除")
    @ApiOperation(value = "成绩修改申请-批量删除", notes = "成绩修改申请-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwCjChangeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "成绩修改申请-通过id查询")
    @ApiOperation(value = "成绩修改申请-通过id查询", notes = "成绩修改申请-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwCjChange veJwCjChange = veJwCjChangeService.getById(id);
        if (veJwCjChange == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwCjChange);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param veJwCjChange
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwCjChange veJwCjChange) {
        return super.exportXls(request, veJwCjChange, VeJwCjChange.class, "成绩修改申请");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwCjChange.class);
    }

}
